package com.wteam.clightmini.serviceImpl;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpStatus;
import com.wteam.system.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.clightmini.pojo.Test;
import com.wteam.clightmini.mapper.TestMapper;
import com.wteam.clightmini.service.TestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.system.common.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许则丕
 * @since 2022-07-12
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    @Autowired
    private TestMapper testMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage add(Test test) {
        ResultMessage resultMessage = new ResultMessage();
        testMapper.insert(test);
        resultMessage.setStatus(HttpStatus.HTTP_OK);
        resultMessage.setMessage("新增成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage edit(Test test) {
        ResultMessage resultMessage = new ResultMessage();
        testMapper.updateById(test);
        resultMessage.setStatus(HttpStatus.HTTP_OK);
        resultMessage.setMessage("编辑成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delete(Long id) {
        ResultMessage resultMessage = new ResultMessage();
        testMapper.deleteById(id);
        resultMessage.setStatus(HttpStatus.HTTP_OK);
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    public ResultMessage page(PageInfo pageInfo) {
        ResultMessage resultMessage = new ResultMessage();
        try {
            if (pageInfo == null){
                throw new IllegalArgumentException("The pageInfo must not be null");
            }
            QueryWrapper<Test> QueryWrapper = new QueryWrapper<>();
            QueryWrapper.orderByAsc("id");

            Page<Test> testPage = new Page<>(pageInfo.getIndexPageNum(),pageInfo.getSize());
            IPage<Test> testPage1 = testMapper.selectPage(testPage, QueryWrapper);

            Map<String, Object> map = MapUtil.builder(new HashMap<String, Object>())
                    .put("size", testPage1.getSize())
                    .put("indexPageNum", testPage1.getCurrent())
                    .put("totalCount", testPage1.getTotal())
                    .put("testList", testPage1.getRecords()).map();

            resultMessage.setStatus(HttpStatus.HTTP_OK);
            resultMessage.setMessage("获取成功");
            resultMessage.setResultParam(map);

        }catch (IllegalArgumentException e) {
            e.printStackTrace();
            resultMessage.setMessage("参数不正确");
            resultMessage.setStatus(HttpStatus.HTTP_BAD_REQUEST);
        } catch (Exception e) {
            e.printStackTrace();
            resultMessage.setMessage("获取失败");
            resultMessage.setStatus(HttpStatus.HTTP_BAD_REQUEST);
        }
        return resultMessage;
    }
}
